/**
 * UploadDocAction.java
 */
package com.prs.crm.action.doc;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.Enumeration;
import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.courser.ui.TreeNode;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.prs.crm.annotation.Clew;
import com.prs.crm.context.AppContext;
import com.prs.crm.context.FileUtil;
import com.prs.crm.domain.doc.UploadDoc;
import com.prs.crm.domain.document.knowledge.Doc;
import com.prs.crm.domain.hr.Person;
import com.prs.crm.domain.sys.DocumentAdmin;
import com.prs.crm.domain.sys.FileTypeMaintain;
import com.prs.crm.domain.warning.WarnContent;
import com.prs.crm.service.doc.UploadDocService;
import com.prs.crm.service.hr.PersonService;
import com.prs.crm.service.sys.DocumentAdminService;
import com.prs.crm.service.sys.FileTypeMaintainService;
import com.prs.crm.service.warning.WarnContentService;
import com.prs.crm.tools.ControlJMail;
import com.prs.crm.tools.Datetools;
import com.prs.crm.tools.NoteTools;
import com.prs.defaultTools.PrsToolKit;

/**
 * @author ZhuDongMei
 * @E-mail:1223984339@qq.com
 * @qq:1223984339
 * @version 创建时间：2011-3-16 上午11:12:28 简单说明
 */

public class UploadDocAction extends BaseAction {
	static final int BUFFER = 1024;
	private static final long serialVersionUID = 1L;
	private UploadDoc uploadDoc;
	private UploadDocService uploadDocService;
	private PaginationSupport pagination;
	private Collection<TreeNode> nodes;
	private File upload;
	private File uplodS;
	private String uploadFileName;
	private String uplodSFileName;
	private String UploadDocIds;
	private FileTypeMaintainService fileTypeMaintainService;
	private ControlJMail cj=new ControlJMail();
	private NoteTools nt=new NoteTools();
	private Datetools dt=new Datetools();
	private PersonService personService;
	private WarnContentService warnContentService;
	private DocumentAdminService documentAdminService;
	public DocumentAdminService getDocumentAdminService() {
		return documentAdminService;
	}

	public void setDocumentAdminService(DocumentAdminService documentAdminService) {
		this.documentAdminService = documentAdminService;
	}

	public ControlJMail getCj() {
		return cj;
	}

	public void setCj(ControlJMail cj) {
		this.cj = cj;
	}

	public NoteTools getNt() {
		return nt;
	}

	public void setNt(NoteTools nt) {
		this.nt = nt;
	}

	public Datetools getDt() {
		return dt;
	}

	public void setDt(Datetools dt) {
		this.dt = dt;
	}

	public PersonService getPersonService() {
		return personService;
	}

	public void setPersonService(PersonService personService) {
		this.personService = personService;
	}

	public WarnContentService getWarnContentService() {
		return warnContentService;
	}

	public void setWarnContentService(WarnContentService warnContentService) {
		this.warnContentService = warnContentService;
	}

	public FileTypeMaintainService getFileTypeMaintainService() {
		return fileTypeMaintainService;
	}

	public String getUplodSFileName() {
		return uplodSFileName;
	}

	public void setUplodSFileName(String uplodSFileName) {
		this.uplodSFileName = uplodSFileName;
	}

	public void setFileTypeMaintainService(
			FileTypeMaintainService fileTypeMaintainService) {
		this.fileTypeMaintainService = fileTypeMaintainService;
	}

	public Collection<TreeNode> getNodes() {
		return nodes;
	}

	public void setNodes(Collection<TreeNode> nodes) {
		this.nodes = nodes;
	}

	public File getUpload() {
		return upload;
	}

	public void setUpload(File upload) {
		this.upload = upload;
	}

	public File getUplodS() {
		return uplodS;
	}

	public void setUplodS(File uplodS) {
		this.uplodS = uplodS;
	}

	public String getUploadFileName() {
		return uploadFileName;
	}

	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}

	public String getUploadDocIds() {
		return UploadDocIds;
	}

	public void setUploadDocIds(String uploadDocIds) {
		UploadDocIds = uploadDocIds;
	}

	public UploadDoc getUploadDoc() {
		return uploadDoc;
	}

	public void setUploadDoc(UploadDoc uploadDoc) {
		this.uploadDoc = uploadDoc;
	}

	public UploadDocService getUploadDocService() {
		return uploadDocService;
	}

	public void setUploadDocService(UploadDocService uploadDocService) {
		this.uploadDocService = uploadDocService;
	}

	public PaginationSupport getPagination() {
		return pagination;
	}

	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}

	@Action("listLoad")
	public String listLoad() {
		Person cuPerson=AppContext.getAccountContext().currentUser();
		List<UploadDoc> d=this.getUploadDocService().getAllUploadDocs();
		for (UploadDoc ob : d) {
			if (ob.getRecorder().getResume().getName().equals(cuPerson.getResume().getName())) {
				ob.setViState(1);//1表示有访问权限，2没有访问权限
				ob.setUpState(1);//1表示有修改权限，2没有修改权限
				this.getUploadDocService().submit(ob);
			}else{
				if (ob.getPersonId()==null || "".equals(ob.getPersonId().trim())) {
					ob.setUpState(1);
					this.getUploadDocService().submit(ob);
				}else{
					String[] per=ob.getPersonId().split(",");
					for (String pers : per) {
						if (cuPerson.getId().equals(Integer.parseInt(pers))) {
							ob.setUpState(1);
							this.getUploadDocService().submit(ob);
							break;
						}else{
							ob.setUpState(2);
							this.getUploadDocService().submit(ob);
						}
					}	
				}
				if (ob.getVisitId()==null || "".equals(ob.getVisitId().trim())) {
					ob.setViState(1);
					this.getUploadDocService().submit(ob);
				}else{
					String[] per=ob.getVisitId().split(",");
					for (String pers : per) {
						if (cuPerson.getId().equals(Integer.parseInt(pers))) {
							ob.setViState(1);
							this.getUploadDocService().submit(ob);
							break;
						}else{
							ob.setViState(2);
							this.getUploadDocService().submit(ob);
						}
					}
				}
			}
			DocumentAdmin da=this.getDocumentAdminService().getDocumentAdmin("dm01");
			if (da!=null) {
				if (da.getPersonNameId()==null || "".equals(da.getPersonNameId().trim())) {
					ob.setDelState(2);
					this.getUploadDocService().submit(ob);
				}else{
					String[] per=da.getPersonNameId().split(",");
					for (String pers : per) {
						if (cuPerson.getId().equals(Integer.parseInt(pers))) {
							ob.setDelState(1);
							this.getUploadDocService().submit(ob);
							break;
						}else{
							ob.setDelState(2);
							this.getUploadDocService().submit(ob);
						}
					}
				}
			}
		}
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				UploadDoc.class).add(Restrictions.isNull("parent"));
		criteria.add(Restrictions.eq("viState", 1));
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getUploadDocService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}

	@Action("listUploadCenter")
	public String listUploadCenter() {
		Person cuPerson=AppContext.getAccountContext().currentUser();
		List<UploadDoc> d=this.getUploadDocService().getAllUploadDocs();
		for (UploadDoc ob : d) {
			if (ob.getRecorder().getResume().getName().equals(cuPerson.getResume().getName())) {
				ob.setViState(1);//1表示有访问权限，2没有访问权限
				ob.setUpState(1);//1表示有修改权限，2没有修改权限
				this.getUploadDocService().submit(ob);
			}else{
				if (ob.getPersonId()==null || "".equals(ob.getPersonId().trim())) {
					ob.setUpState(1);
					this.getUploadDocService().submit(ob);
				}else{
					String[] per=ob.getPersonId().split(",");
					for (String pers : per) {
						if (cuPerson.getId().equals(Integer.parseInt(pers))) {
							ob.setUpState(1);
							this.getUploadDocService().submit(ob);
							break;
						}else{
							ob.setUpState(2);
							this.getUploadDocService().submit(ob);
						}
					}	
				}
				if (ob.getVisitId()==null || "".equals(ob.getVisitId().trim())) {
					ob.setViState(1);
					this.getUploadDocService().submit(ob);
				}else{
					String[] per=ob.getVisitId().split(",");
					for (String pers : per) {
						if (cuPerson.getId().equals(Integer.parseInt(pers))) {
							ob.setViState(1);
							this.getUploadDocService().submit(ob);
							break;
						}else{
							ob.setViState(2);
							this.getUploadDocService().submit(ob);
						}
					}
				}
			}
			DocumentAdmin da=this.getDocumentAdminService().getDocumentAdmin("dm01");
			if (da!=null) {
				if (da.getPersonNameId()==null || "".equals(da.getPersonNameId().trim())) {
					ob.setDelState(2);
					this.getUploadDocService().submit(ob);
				}else{
					String[] per=da.getPersonNameId().split(",");
					for (String pers : per) {
						if (cuPerson.getId().equals(Integer.parseInt(pers))) {
							ob.setDelState(1);
							this.getUploadDocService().submit(ob);
							break;
						}else{
							ob.setDelState(2);
							this.getUploadDocService().submit(ob);
						}
					}
				}
			}
		}
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				UploadDoc.class).add(Restrictions.isNull("parent"));
		criteria.add(Restrictions.eq("viState", 1));
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getUploadDocService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}

	@Action("listUploadCenterTree")
	public String listUploadCenterTree() {
		Person cuPerson=AppContext.getAccountContext().currentUser();
		List<UploadDoc> d=this.getUploadDocService().getAllUploadDocs();
		for (UploadDoc ob : d) {
			if (ob.getRecorder().getResume().getName().equals(cuPerson.getResume().getName())) {
				ob.setViState(1);//1表示有访问权限，2没有访问权限
				ob.setUpState(1);//1表示有修改权限，2没有修改权限
				this.getUploadDocService().submit(ob);
			}else{
				if (ob.getPersonId()==null || "".equals(ob.getPersonId().trim())) {
					ob.setUpState(1);
					this.getUploadDocService().submit(ob);
				}else{
					String[] per=ob.getPersonId().split(",");
					for (String pers : per) {
						if (cuPerson.getId().equals(Integer.parseInt(pers))) {
							ob.setUpState(1);
							this.getUploadDocService().submit(ob);
							break;
						}else{
							ob.setUpState(2);
							this.getUploadDocService().submit(ob);
						}
					}	
				}
				if (ob.getVisitId()==null || "".equals(ob.getVisitId().trim())) {
					ob.setViState(1);
					this.getUploadDocService().submit(ob);
				}else{
					String[] per=ob.getVisitId().split(",");
					for (String pers : per) {
						if (cuPerson.getId().equals(Integer.parseInt(pers))) {
							ob.setViState(1);
							this.getUploadDocService().submit(ob);
							break;
						}else{
							ob.setViState(2);
							this.getUploadDocService().submit(ob);
						}
					}
				}
			}
			DocumentAdmin da=this.getDocumentAdminService().getDocumentAdmin("dm01");
			if (da!=null) {
				if (da.getPersonNameId()==null || "".equals(da.getPersonNameId().trim())) {
					ob.setDelState(2);
					this.getUploadDocService().submit(ob);
				}else{
					String[] per=da.getPersonNameId().split(",");
					for (String pers : per) {
						if (cuPerson.getId().equals(Integer.parseInt(pers))) {
							ob.setDelState(1);
							this.getUploadDocService().submit(ob);
							break;
						}else{
							ob.setDelState(2);
							this.getUploadDocService().submit(ob);
						}
					}
				}
			}
		}
		return SUCCESS;
	}

	@Action("listUploadCenterByFolder")
	public String listUploadCenterByFolder() {
		Person cuPerson=AppContext.getAccountContext().currentUser();
		List<UploadDoc> d=this.getUploadDocService().getAllUploadDocs();
		for (UploadDoc ob : d) {
			if (ob.getRecorder().getResume().getName().equals(cuPerson.getResume().getName())) {
				ob.setViState(1);//1表示有访问权限，2没有访问权限
				ob.setUpState(1);//1表示有修改权限，2没有修改权限
				this.getUploadDocService().submit(ob);
			}else{
				if (ob.getPersonId()==null || "".equals(ob.getPersonId().trim())) {
					ob.setUpState(1);
					this.getUploadDocService().submit(ob);
				}else{
					String[] per=ob.getPersonId().split(",");
					for (String pers : per) {
						if (cuPerson.getId().equals(Integer.parseInt(pers))) {
							ob.setUpState(1);
							this.getUploadDocService().submit(ob);
							break;
						}else{
							ob.setUpState(2);
							this.getUploadDocService().submit(ob);
						}
					}	
				}
				if (ob.getVisitId()==null || "".equals(ob.getVisitId().trim())) {
					ob.setViState(1);
					this.getUploadDocService().submit(ob);
				}else{
					String[] per=ob.getVisitId().split(",");
					for (String pers : per) {
						if (cuPerson.getId().equals(Integer.parseInt(pers))) {
							ob.setViState(1);
							this.getUploadDocService().submit(ob);
							break;
						}else{
							ob.setViState(2);
							this.getUploadDocService().submit(ob);
						}
					}
				}
			}
			DocumentAdmin da=this.getDocumentAdminService().getDocumentAdmin("dm01");
			if (da!=null) {
				if (da.getPersonNameId()==null || "".equals(da.getPersonNameId().trim())) {
					ob.setDelState(2);
					this.getUploadDocService().submit(ob);
				}else{
					String[] per=da.getPersonNameId().split(",");
					for (String pers : per) {
						if (cuPerson.getId().equals(Integer.parseInt(pers))) {
							ob.setDelState(1);
							this.getUploadDocService().submit(ob);
							break;
						}else{
							ob.setDelState(2);
							this.getUploadDocService().submit(ob);
						}
					}
				}
			}
		}
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				UploadDoc.class).add(Restrictions.isNull("parent"));
		criteria.add(Restrictions.eq("viState", 1));
		// criteria.add(Restrictions.in("parent",this.getUploadDocService().convertToUploadDoc(UploadDocIds)));
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getUploadDocService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}

	@Action("listUploadCenterByFolders")
	public String listUploadCenterByFolders() {
		Person cuPerson=AppContext.getAccountContext().currentUser();
		List<UploadDoc> d=this.getUploadDocService().getAllUploadDocs();
		for (UploadDoc ob : d) {
			if (ob.getRecorder().getResume().getName().equals(cuPerson.getResume().getName())) {
				ob.setViState(1);//1表示有访问权限，2没有访问权限
				ob.setUpState(1);//1表示有修改权限，2没有修改权限
				this.getUploadDocService().submit(ob);
			}else{
				if (ob.getPersonId()==null || "".equals(ob.getPersonId().trim())) {
					ob.setUpState(1);
					this.getUploadDocService().submit(ob);
				}else{
					String[] per=ob.getPersonId().split(",");
					for (String pers : per) {
						if (cuPerson.getId().equals(Integer.parseInt(pers))) {
							ob.setUpState(1);
							this.getUploadDocService().submit(ob);
							break;
						}else{
							ob.setUpState(2);
							this.getUploadDocService().submit(ob);
						}
					}	
				}
				if (ob.getVisitId()==null || "".equals(ob.getVisitId().trim())) {
					ob.setViState(1);
					this.getUploadDocService().submit(ob);
				}else{
					String[] per=ob.getVisitId().split(",");
					for (String pers : per) {
						if (cuPerson.getId().equals(Integer.parseInt(pers))) {
							ob.setViState(1);
							this.getUploadDocService().submit(ob);
							break;
						}else{
							ob.setViState(2);
							this.getUploadDocService().submit(ob);
						}
					}
				}
			}
			DocumentAdmin da=this.getDocumentAdminService().getDocumentAdmin("dm01");
			if (da!=null) {
				if (da.getPersonNameId()==null || "".equals(da.getPersonNameId().trim())) {
					ob.setDelState(2);
					this.getUploadDocService().submit(ob);
				}else{
					String[] per=da.getPersonNameId().split(",");
					for (String pers : per) {
						if (cuPerson.getId().equals(Integer.parseInt(pers))) {
							ob.setDelState(1);
							this.getUploadDocService().submit(ob);
							break;
						}else{
							ob.setDelState(2);
							this.getUploadDocService().submit(ob);
						}
					}
				}
			}
		}
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				UploadDoc.class).add(Restrictions.isNotNull("parent")).add(
				Restrictions.eq("parent", uploadDoc));
		criteria.add(Restrictions.eq("viState", 1));
		// criteria.add(Restrictions.in("parent",this.getUploadDocService().convertToUploadDoc(UploadDocIds)));
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getUploadDocService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}

	@Action(value = "loadUploadCenterTree", results = { @Result(name = "success", type = "json", params = {
			"root", "nodes" }) })
	public String loadUploadCenterTree() {
		nodes = new ArrayList<TreeNode>();
		Collection<UploadDoc> uploadDocs = this.getUploadDocService()
				.getRootUploadDoc();
		for (UploadDoc uploadDoc : uploadDocs) {
			TreeNode node = new TreeNode();
			node.setId(String.valueOf(uploadDoc.getId()));
			node.setText(uploadDoc.getName());
			addChildNode(uploadDoc, node);
			nodes.add(node);
		}
		return SUCCESS;
	}

	private void addChildNode(UploadDoc uploadDoc, TreeNode parentnode) {
		if (uploadDoc.getChilds() != null) {
			for (UploadDoc document : uploadDoc.getChilds()) {
				if ("文件夹".equals(document.getType().trim())) {
					TreeNode node = new TreeNode();
					node.setId(String.valueOf(document.getId()));
					node.setText(document.getName());
					parentnode.addChildNode(node);
					addChildNode(document, node);
				}
			}
		}
	}

	@Action(value = "loadUploadCenterTrees", results = { @Result(name = "success", type = "json", params = {
			"root", "nodes" }) })
	public String loadUploadCenterTrees() {
		nodes = new ArrayList<TreeNode>();
		Collection<UploadDoc> uploadDocs = this.getUploadDocService()
				.getRootUploadDoc();
		for (UploadDoc uploadDoc : uploadDocs) {
			if (uploadDoc.getUpState()==1) {
				TreeNode node = new TreeNode();
				node.setId(String.valueOf(uploadDoc.getId()));
				node.setText(uploadDoc.getName());
				addChildNodes(uploadDoc, node);
				nodes.add(node);
			}
		}
		return SUCCESS;
	}

	private void addChildNodes(UploadDoc doc, TreeNode parentnode) {
		if (doc.getChilds() != null) {
			for (UploadDoc document : doc.getChilds()) {
				if (document.getType().equals("文件夹")) {
					if (document.getUpState()==1) {
						TreeNode node = new TreeNode();
						node.setId(String.valueOf(document.getId()));
						node.setText(document.getName());
						parentnode.addChildNode(node);
						addChildNodes(document, node);
					}
				}
			}
		}
	}

	@Clew(results = { "error" })
	@Action(value = "removeUpload", results = {
			@Result(name = "success", type = "redirect", location = "listUploadCenter"),
			@Result(name = "error", type = "redirect", location = "listUploadCenter") })
	public String removeUpload() {
		if (uploadDoc.getChilds().size() != 0) {
			this.setPrompt("该文件下面有关联文件，不能删除该文件！");
			return ERROR;
		} else {
			this.getUploadDocService().removeUploadDoc(uploadDoc.getId());
			return SUCCESS;
		}
	}

	@Clew(results = { "error" })
	@Action(value = "removeUploads", results = {
			@Result(name = "success", type = "redirect", location = "listUploadCenterTree"),
			@Result(name = "error", type = "redirect", location = "listUploadCenterTree") })
	public String removeUploads() {
		if (uploadDoc.getChilds().size() != 0) {
			this.setPrompt("该文件下面有关联文件，不能删除该文件！");
			return ERROR;
		} else {
			this.getUploadDocService().removeUploadDoc(uploadDoc.getId());
			return SUCCESS;
		}
	}
	@Action("listUpload")
	public String listUpload() {
		Person cuPerson=AppContext.getAccountContext().currentUser();
		List<UploadDoc> d=this.getUploadDocService().getAllUploadDocs();
		for (UploadDoc ob : d) {
			if (ob.getRecorder().getResume().getName().equals(cuPerson.getResume().getName())) {
				ob.setViState(1);//1表示有访问权限，2没有访问权限
				ob.setUpState(1);//1表示有修改权限，2没有修改权限
				this.getUploadDocService().submit(ob);
			}else{
				if (ob.getPersonId()==null || "".equals(ob.getPersonId().trim())) {
					ob.setUpState(1);
					this.getUploadDocService().submit(ob);
				}else{
					String[] per=ob.getPersonId().split(",");
					for (String pers : per) {
						if (cuPerson.getId().equals(Integer.parseInt(pers))) {
							ob.setUpState(1);
							this.getUploadDocService().submit(ob);
							break;
						}else{
							ob.setUpState(2);
							this.getUploadDocService().submit(ob);
						}
					}	
				}
				if (ob.getVisitId()==null || "".equals(ob.getVisitId().trim())) {
					ob.setViState(1);
					this.getUploadDocService().submit(ob);
				}else{
					String[] per=ob.getVisitId().split(",");
					for (String pers : per) {
						if (cuPerson.getId().equals(Integer.parseInt(pers))) {
							ob.setViState(1);
							this.getUploadDocService().submit(ob);
							break;
						}else{
							ob.setViState(2);
							this.getUploadDocService().submit(ob);
						}
					}
				}
			}
			DocumentAdmin da=this.getDocumentAdminService().getDocumentAdmin("dm01");
			if (da!=null) {
				if (da.getPersonNameId()==null || "".equals(da.getPersonNameId().trim())) {
					ob.setDelState(2);
					this.getUploadDocService().submit(ob);
				}else{
					String[] per=da.getPersonNameId().split(",");
					for (String pers : per) {
						if (cuPerson.getId().equals(Integer.parseInt(pers))) {
							ob.setDelState(1);
							this.getUploadDocService().submit(ob);
							break;
						}else{
							ob.setDelState(2);
							this.getUploadDocService().submit(ob);
						}
					}
				}
			}
		}
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				UploadDoc.class).add(
				Restrictions.eq("parent", this.getUploadDocService()
						.getUploadDocById(uploadDoc.getId())));
		criteria.add(Restrictions.eq("viState", 1));
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getUploadDocService().listByPage(criteria,
				pagination));
		return SUCCESS;

	}

	@Action("listUploadTree")
	public String listUploadTree() {
		Person cuPerson=AppContext.getAccountContext().currentUser();
		List<UploadDoc> d=this.getUploadDocService().getAllUploadDocs();
		for (UploadDoc ob : d) {
			if (ob.getRecorder().getResume().getName().equals(cuPerson.getResume().getName())) {
				ob.setViState(1);//1表示有访问权限，2没有访问权限
				ob.setUpState(1);//1表示有修改权限，2没有修改权限
				this.getUploadDocService().submit(ob);
			}else{
				if (ob.getPersonId()==null || "".equals(ob.getPersonId().trim())) {
					ob.setUpState(1);
					this.getUploadDocService().submit(ob);
				}else{
					String[] per=ob.getPersonId().split(",");
					for (String pers : per) {
						if (cuPerson.getId().equals(Integer.parseInt(pers))) {
							ob.setUpState(1);
							this.getUploadDocService().submit(ob);
							break;
						}else{
							ob.setUpState(2);
							this.getUploadDocService().submit(ob);
						}
					}	
				}
				if (ob.getVisitId()==null || "".equals(ob.getVisitId().trim())) {
					ob.setViState(1);
					this.getUploadDocService().submit(ob);
				}else{
					String[] per=ob.getVisitId().split(",");
					for (String pers : per) {
						if (cuPerson.getId().equals(Integer.parseInt(pers))) {
							ob.setViState(1);
							this.getUploadDocService().submit(ob);
							break;
						}else{
							ob.setViState(2);
							this.getUploadDocService().submit(ob);
						}
					}
				}
			}
			DocumentAdmin da=this.getDocumentAdminService().getDocumentAdmin("dm01");
			if (da!=null) {
				if (da.getPersonNameId()==null || "".equals(da.getPersonNameId().trim())) {
					ob.setDelState(2);
					this.getUploadDocService().submit(ob);
				}else{
					String[] per=da.getPersonNameId().split(",");
					for (String pers : per) {
						if (cuPerson.getId().equals(Integer.parseInt(pers))) {
							ob.setDelState(1);
							this.getUploadDocService().submit(ob);
							break;
						}else{
							ob.setDelState(2);
							this.getUploadDocService().submit(ob);
						}
					}
				}
			}
		}
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				UploadDoc.class).add(
				Restrictions.eq("parent", this.getUploadDocService()
						.getUploadDocById(uploadDoc.getId())));
		criteria.add(Restrictions.eq("viState", 1));
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getUploadDocService().listByPage(criteria,
				pagination));
		return SUCCESS;

	}

	@Action(value = "volumeUpload", results = {
			@Result(name = "success", type = "redirect", location = "listUploadCenter"),
			@Result(name = "error", type = "chain", location = "editVolumeUpload") })
	public String volumeUpload() throws IOException {
		if (uplodS != null) {
			String str = uplodSFileName.substring(uplodSFileName.length() - 3,
					uplodSFileName.length());
			if ("zip".equals(str)) {
				long size = new FileInputStream(new File(uplodS.getPath()))
						.available();
				if ("0".equals(AppContext.getAccountContext().currentUser()
						.getUploadMaxVolume())
						|| "".equals(AppContext.getAccountContext()
								.currentUser().getUploadMaxVolume())) {
					success();
					return SUCCESS;
				} else {
					long max = new Double(Double.parseDouble(AppContext
							.getAccountContext().currentUser()
							.getUploadMaxVolume()) * 1024 * 1024).longValue();

					if (size <= max) {
						success();
						return SUCCESS;
					} else {
						this.addActionError("上传的附件太大！");
						return ERROR;
					}
				}

			} else {
				this.addActionError("上传的文件必须是ZIP压缩文件！");
				return ERROR;
			}
		} else {
			this.addActionError("上传的附件不能为空！");
			return ERROR;
		}
	}

	public void success() {
		try {
			unZip(uplodS.getAbsolutePath(), "C://abc//");

			File f = new File("C://abc//");
			File[] fs = f.listFiles();
			List<UploadDoc> uploadDocs = new ArrayList<UploadDoc>();
			for (int i = 0; i < fs.length; i++) {
				UploadDoc uploadDoc1 = new UploadDoc();
				System.out.println(fs[i].getName());
				if (!fs[i].isDirectory()) {
					uploadDoc1.setAttach(FileUtil.getInstance().createAttach(
							fs[i], fs[i].getName()));
					List<FileTypeMaintain> ft = this
							.getFileTypeMaintainService().getFileTypeMaintain(
									uploadDoc1.getAttach().getContentType());
					if (ft.size() > 0) {
						for (FileTypeMaintain fileTypeMaintain : ft) {
							uploadDoc1.setFileTypeMaintain(fileTypeMaintain);
							uploadDoc1.setType("other");
							uploadDoc1.setTime(new Date());
							uploadDoc1
									.setSize(uploadDoc1.getAttach().getSize());
							uploadDoc1.setParent(uploadDoc.getParent());
							uploadDoc1.setName(fs[i].getName());
							uploadDocs.add(uploadDoc1);
						}
					} else {
						uploadDoc1.setType("others");
						uploadDoc1.setTime(new Date());
						uploadDoc1.setSize(uploadDoc1.getAttach().getSize());
						uploadDoc1.setParent(uploadDoc.getParent());
						uploadDoc1.setName(fs[i].getName());
						uploadDocs.add(uploadDoc1);
					}
				} else {
					readFile(fs[i]);

				}

			}

			for (UploadDoc ud : uploadDocs) {
				this.getUploadDocService().save(ud);
			}
			deleteAll(f);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public void readFile(File file) throws IOException {
		File[] files = file.listFiles();
		System.out.println(files.length);
		List<UploadDoc> uploadDocs = new ArrayList<UploadDoc>();
		for (int i = 0; i < files.length; i++) {
			UploadDoc uploadDoc2 = new UploadDoc();
			System.out.println(files[i].getName());
			if (!files[i].isDirectory()) {
				uploadDoc2.setAttach(FileUtil.getInstance().createAttach(
						files[i], files[i].getName()));
				List<FileTypeMaintain> ft = this.getFileTypeMaintainService()
						.getFileTypeMaintain(
								uploadDoc2.getAttach().getContentType());
				if (ft.size() > 0) {
					for (FileTypeMaintain fileTypeMaintain : ft) {
						uploadDoc2.setFileTypeMaintain(fileTypeMaintain);
						uploadDoc2.setType("other");
						uploadDoc2.setTime(new Date());
						uploadDoc2.setSize(uploadDoc2.getAttach().getSize());
						uploadDoc2.setParent(uploadDoc.getParent());
						uploadDoc2.setName(files[i].getName());
						uploadDocs.add(uploadDoc2);
					}
				} else {
					uploadDoc2.setType("others");
					uploadDoc2.setTime(new Date());
					uploadDoc2.setSize(uploadDoc2.getAttach().getSize());
					uploadDoc2.setParent(uploadDoc.getParent());
					uploadDoc2.setName(files[i].getName());
					uploadDocs.add(uploadDoc2);
				}
			} else {
				readFile(files[i]);

			}
		}
		for (UploadDoc ud : uploadDocs) {
			this.getUploadDocService().save(ud);
		}
	}

	public void deleteAll(File file) {
		File[] fs = file.listFiles();
		for (int i = 0; i < fs.length; i++) {
			if (fs[i].isDirectory()) {
				deleteAll(fs[i]);
			} else {
				fs[i].delete();
			}
		}
		file.delete();
	}

	@SuppressWarnings("unchecked")
	public static void unZip(String fileName, String filePath) throws Exception {
		ZipFile zipFile = new ZipFile(fileName, "GBK"); // 以“GBK”编码创建zip文件，用来处理winRAR压缩的文件。
		Enumeration emu = zipFile.getEntries();

		while (emu.hasMoreElements()) {
			ZipEntry entry = (ZipEntry) emu.nextElement();
			if (entry.isDirectory()) {
				new File(filePath + entry.getName()).mkdirs();
				continue;
			}
			BufferedInputStream bis = new BufferedInputStream(zipFile
					.getInputStream(entry));

			File file = new File(filePath + entry.getName());
			File parent = file.getParentFile();
			if (parent != null && (!parent.exists())) {
				parent.mkdirs();
			}
			FileOutputStream fos = new FileOutputStream(file);
			BufferedOutputStream bos = new BufferedOutputStream(fos, BUFFER);

			byte[] buf = new byte[BUFFER];
			int len = 0;
			while ((len = bis.read(buf, 0, BUFFER)) != -1) {
				fos.write(buf, 0, len);
			}
			bos.flush();
			bos.close();
			bis.close();
		}
		zipFile.close();
	}

	@Action(value = "upload", results = {
			@Result(name = "success", type = "redirect", location = "listUploadCenter"),
			@Result(name = "error", type = "chain", location = "editUpload") })
	public String upload() {
		if ("".equals(uploadDoc.getName())) {
			this.addActionError("文件主题不能为空！");
			return ERROR;
		}
		if (upload != null) {
			File file = new File(upload.getPath());
			try {
				long sizes = new FileInputStream(file).available();
				if ("0".equals(AppContext.getAccountContext().currentUser()
						.getUploadMaxVolume())
						|| "".equals(AppContext.getAccountContext()
								.currentUser().getUploadMaxVolume())) {
					this.uploadDoc.setAttach(FileUtil.getInstance()
							.createAttach(upload, uploadFileName));
					List<FileTypeMaintain> ft = this
							.getFileTypeMaintainService().getFileTypeMaintain(
									uploadDoc.getAttach().getContentType());
					if (ft.size() > 0) {
						for (FileTypeMaintain fileTypeMaintain : ft) {
							uploadDoc.setFileTypeMaintain(fileTypeMaintain);
							uploadDoc.setType("other");
							uploadDoc.setTime(new Date());
						}
					} else {
						uploadDoc.setType("others");
					}
					uploadDoc.setUpPerson(AppContext.getAccountContext().currentUser());
					this.getUploadDocService().save(uploadDoc);
					return SUCCESS;
				} else {
					long max = new Double(Double.parseDouble(AppContext
							.getAccountContext().currentUser()
							.getUploadMaxVolume()) * 1024 * 1024).longValue();
					if (sizes <= max) {
						this.uploadDoc.setAttach(FileUtil.getInstance()
								.createAttach(upload, uploadFileName));
						List<FileTypeMaintain> ft = this
								.getFileTypeMaintainService()
								.getFileTypeMaintain(
										uploadDoc.getAttach().getContentType());
						if (ft.size() > 0) {
							for (FileTypeMaintain fileTypeMaintain : ft) {
								uploadDoc.setFileTypeMaintain(fileTypeMaintain);
								uploadDoc.setType("other");
								uploadDoc.setTime(new Date());
							}
						} else {
							uploadDoc.setType("others");
						}
						uploadDoc.setUpPerson(AppContext.getAccountContext().currentUser());
						this.getUploadDocService().save(uploadDoc);
						return SUCCESS;
					} else {
						this.addActionError("上传的附件太大！");
						return ERROR;
					}
				}
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} else {
			this.addActionError("上传的附件不能为空！");
			return ERROR;
		}
		uploadDoc.setUpPerson(AppContext.getAccountContext().currentUser());
		this.getUploadDocService().save(uploadDoc);
		return SUCCESS;
	}

	@Action("editFolder")
	public String editFolder() {
		return SUCCESS;
	}

	@Action("editSearch")
	public String editSearch() {
		return SUCCESS;
	}

	@Action("editUpload")
	public String editUpload() {
		return SUCCESS;
	}

	@Action("editVolumeUpload")
	public String editVolumeUpload() {
		return SUCCESS;
	}

	@Action("editFile")
	public String editFile() {
		return SUCCESS;
	}

	@Action("editQuery")
	public String editQuery() {
		return SUCCESS;
	}

	@Action("editRenameFolder")
	public String editRenameFolder() {
		return SUCCESS;
	}

	@Action(value = "saveFile", results = {
			@Result(name = "success", type = "redirect", location = "listUploadCenter"),
			@Result(name = "error", type = "chain", location = "editFile") })
	public String saveFile() {
		if ("".equals(uploadDoc.getName())) {
			this.addActionError("文件主题不能为空！");
			return ERROR;
		}
		uploadDoc.setTime(new Date());
		uploadDoc.setUpPerson(AppContext.getAccountContext().currentUser());
		this.getUploadDocService().save(uploadDoc);
		return SUCCESS;
	}

	@Action(value = "saveFolder", results = {
			@Result(name = "success", type = "redirect", location = "listUploadCenter"),
			@Result(name = "error", type = "chain", location = "editFolder") })
	public String saveFolder() {
		if ("".equals(uploadDoc.getName())) {
			this.addActionError("文件夹名称不能为空！");
			return ERROR;
		}
		uploadDoc.setTime(new Date());
		uploadDoc.setType("文件夹");
		uploadDoc.setUpPerson(AppContext.getAccountContext().currentUser());
		this.getUploadDocService().save(uploadDoc);
		if (uploadDoc.getId()!=null) {
			if ("给指定人员发邮件".equals(uploadDoc.getUpdateSet())) {
				String[] perId=uploadDoc.getAppiontPersonId().split(",");
				for (String per : perId) {
					Person person=this.getPersonService().getPersonById(Integer.parseInt(per));
					Person curper=AppContext.getAccountContext().getUser();
					if (person!=null) {
						cj.sendMail(curper.getResume().getEmail(), person.getResume().getEmail(), person.getLoginCode(), person.getLoginPwd(), "文件夹更新信息", "文件夹"+uploadDoc.getName()+"已更新");
					}
				}
			}
			if ("给指定人员发送手机短信".equals(uploadDoc.getUpdateSet())) {
				String[] perId=uploadDoc.getAppiontPersonId().split(",");
				for (String per : perId) {
					Person person=this.getPersonService().getPersonById(Integer.parseInt(per));
					if (person!=null) {
						Date date=new Date();
						SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd HH:mm");
						try {
							nt.SendNote(person.getResume().getMobile_No(),"文件夹"+uploadDoc.getName()+"已更新",sf.format(date));
						} catch (MalformedURLException e) {
							e.printStackTrace();
						} catch (UnsupportedEncodingException e) {
							e.printStackTrace();
						}	
					}
				}
			}
			if ("给指定人员发送内部消息".equals(uploadDoc.getUpdateSet())) {
				String[] perId=uploadDoc.getAppiontPersonId().split(",");
				for (String per : perId) {
					Person person=this.getPersonService().getPersonById(Integer.parseInt(per));
					Person curper=AppContext.getAccountContext().getUser();
					if (person!=null) {
						Date date=new Date();
						SimpleDateFormat sf=new SimpleDateFormat("yyyyMMddHHmmss");
						 WarnContent wc=new WarnContent();	
				    	 wc.setWarnstyl("文件夹创建成功");
				    	 wc.setWarnCont("文件夹"+uploadDoc.getName()+"更新成功");
				    	 wc.setReceivID(per);
				    	 wc.setReadState("1");
					     this.getWarnContentService().save(wc);		
					}
				}
			}
		}
		return SUCCESS;
	}

	@Action("listMatch")
	public String listMatch() {
		return SUCCESS;
	}

	@Action(value = "query", results = {
			@Result(name = "success", type = "redirect", location = "listMatch?uploadDoc=${uploadDoc.getId()}"),
			@Result(name = "error", type = "chain", location = "editQuery") })
	public String query() {
		List<UploadDoc> docs = this.getUploadDocService().getAllUploadDocs();
		int count = 0;
		for (int i = 0; i < docs.size(); i++) {
			if (docs.get(i).getName().equals(uploadDoc.getName())) {
				if (docs.get(i).getRecorder().getResume().getName() == uploadDoc
						.getRecorder().getResume().getName()) {
					count++;
					this.uploadDoc = docs.get(i);
				}
			}
		}
		if (count > 0) {
			System.out.println(uploadDoc.getId());
			return SUCCESS;
		} else {
			return ERROR;
		}

	}
}
